package com.zrf.gulimall.product.app;

import com.zrf.gulimall.common.database.utils.PageUtils;
import com.zrf.gulimall.commnon.core.utils.R;
import com.zrf.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.zrf.gulimall.product.entity.AttrEntity;
import com.zrf.gulimall.product.entity.AttrGroupEntity;
import com.zrf.gulimall.product.service.AttrAttrGroupRelationService;
import com.zrf.gulimall.product.service.AttrGroupService;
import com.zrf.gulimall.product.service.AttrService;
import com.zrf.gulimall.product.service.CategoryService;
import com.zrf.gulimall.product.vo.AttrGroupVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


/**
 * 属性分组
 *
 * @author zrf
 * @email 120662332@qq.com
 * @date 2021-01-23 16:07:19
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
  @Resource
  private AttrGroupService attrGroupService;

  @Resource
  private AttrService attrService;

  @Resource
  private CategoryService categoryService;

  @Resource
  private AttrAttrGroupRelationService attrAttrGroupRelationService;
  /**
   * 列表
   */
  @RequestMapping("/list/{catelogId}")
  //@RequiresPermissions("product:attrgroup:list")
  public R list(@RequestParam Map<String, Object> params,
                @PathVariable("catelogId") Long catelogId) {
    PageUtils page = attrGroupService.queryPage(params, catelogId);
    return R.ok().put("page", page);
  }

  /**
   * 信息
   */
  @RequestMapping("/info/{attrGroupId}")
  //@RequiresPermissions("product:attrgroup:info")
  public R info(@PathVariable("attrGroupId") Long attrGroupId) {
    AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
    Long[] catelogPath = categoryService.findCatelogPath(attrGroup.getCatelogId());
    attrGroup.setCatelogPath(catelogPath);
    return R.ok().put("attrGroup", attrGroup);
  }

  /**
   * 获取分组关联的所有属性
   */
  @GetMapping("/{attrgroupId}/attr/relation")
  //@RequiresPermissions("product:attrgroup:info")
  public R groupCascadeAttr(@PathVariable("attrgroupId") Long attrGroupId) {
    List<AttrEntity> attrGroup = attrService.queryByGroupId(attrGroupId);
    return R.ok().put("data", attrGroup);
  }

  /**
   * 获取分组没有关联的所有属性
   */
  @GetMapping("/{attrgroupId}/noattr/relation")
  //@RequiresPermissions("product:attrgroup:info")
  public R groupNoCascadeAttr(@RequestParam Map<String, Object> params,
                              @PathVariable("attrgroupId") Long attrGroupId) {
    PageUtils pageUtils = attrService.queryNoRelationByGroupId(params, attrGroupId);
    return R.ok().put("page", pageUtils);
  }

  @GetMapping("/{catelogId}/withattr")
  public R getAttrGroupWithAttrs(@PathVariable("catelogId") Long catalogId) {
    List<AttrGroupVo> data = attrGroupService.queryWithAttrs(catalogId);
    return R.ok().put("data", data);
  }

  /**
   * 保存
   */
  @RequestMapping("/save")
  //@RequiresPermissions("product:attrgroup:save")
  public R save(@RequestBody AttrGroupEntity attrGroup) {
    attrGroupService.save(attrGroup);
    return R.ok();
  }

  /**
   * 关联属性
   */
  @RequestMapping("/attr/relation")
  //@RequiresPermissions("product:attrgroup:save")
  public R saveRelation(@RequestBody List<AttrAttrgroupRelationEntity> relationEntity) {
    attrAttrGroupRelationService.saveBatch(relationEntity);
    return R.ok();
  }

  /**
   * 修改
   */
  @RequestMapping("/update")
  //@RequiresPermissions("product:attrgroup:update")
  public R update(@RequestBody AttrGroupEntity attrGroup) {
    attrGroupService.updateById(attrGroup);
    return R.ok();
  }

  /**
   * 删除分组
   */
  @RequestMapping("/delete")
  //@RequiresPermissions("product:attrgroup:delete")
  public R delete(@RequestBody List<Long> attrGroupIds) {
    attrGroupService.delete(attrGroupIds);
    return R.ok();
  }

  /**
   * 移除关联
   */
  @RequestMapping("/attr/relation/delete")
  //@RequiresPermissions("product:attrgroup:delete")
  public R deleteRelation(@RequestBody List<AttrAttrgroupRelationEntity> relationEntities) {
    attrAttrGroupRelationService.delete(relationEntities);
    return R.ok();
  }

}
